﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Lamex.Library;
using Lamex.Library.CommonClass;
using Lamex.Interface;
using Lamex.mSchedule.UI;
using Lamex.Common;
using Lamex.Base;

using System.Data.SqlClient;

namespace Lamex.mMES.UI
{
    public partial class frmMES_Layout_Lamex : frmBaseChild
    {
        //private static string strConn = "Data Source=172.23.134.36;Initial Catalog=master;Integrated Security=False;user=sa;password=Sa@1050%;";
        private static string strConn = SqlConfiguration.CurrentConnectionString;
        private static string UserName = Loginer.CurrentUser.Account;
      
        DataTable dtRGB = null;

        Bitmap imgFront;        //原始图片
        Bitmap imgBack;         //参考图片
        TextBox myText;         //显示信息
        string myInfo;

        public frmMES_Layout_Lamex()
        {
            InitializeComponent();
            //this.pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;
            //this.pictureBox1.BorderStyle = BorderStyle.Fixed3D;
            //this.pictureBox1.Location = new Point(0, 0);
        }

        private void frmMES_Layout_Lamex_Load(object sender, EventArgs e)
        {
            treeView1.Nodes.Add("数字看板");
            addTreeViewRootNode(treeView1);//添加根节点
            addTreeViewSubNode(treeView1);//添加子节点

            Expand_to_Assign_Level();
            //treeView1.ExpandAll();

            //dataGridView1.Height = this.Height;

            dtRGB = Lamex.Base.BaseBuildSql.GetDataTableBySql(" select * From MES_Layout_Block_Color");

            string strPath = @"\\Tx-fs10\tx-fs1\LAMEX\MES\SysSource\Image\Layout";

            //两种加载图片方法，直接加载、实例化
            //两种调用图片方法，外部引用、内部资源
            //pictureBox1.Image = Image.FromFile(strPath + "\\Lamex_Front.png");
            //Image imgFront = Lamex.mMES.Properties.Resources.Lamex_Front;
            imgFront = new Bitmap(strPath + "\\Lamex_Front.png");      
            imgBack = new Bitmap(strPath + "\\Lamex_Back.png");
            pictureBox1.Tag = "Front IMG";
            pictureBox1.Image = imgFront;       //加载图像                   
            pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;

            //this.Width = pictureBox1.Width + (this.Bounds.Width - this.ClientSize.Width);
            //this.Height = pictureBox1.Height + (this.Bounds.Height - this.ClientSize.Height);
            //设置TextBox
            myText = new TextBox();
            this.Controls.Add(myText);
            myText.Multiline = true;
            myText.Enabled = false;
            myText.Parent = pictureBox1;
            //myText.BackColor = Color.FromArgb(150, 0, 0, 0);
            //myText.ForeColor = Color.White;
            myText.Width = 120;
            myText.Height = 64; 
            myText.Visible = true;
        }

        private static DataTable ExcuteQuery(string sql)
        {
            SqlConnection conn = new SqlConnection(strConn);
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlDataAdapter adp = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            adp.Fill(dt);
            return dt;
        }

        //样例：https://www.cnblogs.com/net064/p/5534697.html
        //获取所有数据库，并加载treeView1
        private void addTreeViewRootNode(TreeView tr1)
        {
            string sql = @" select distinct SUB_FACTORY
                            from LamexMESManager.dbo.View_OP_Resource_Plan
                            order by SUB_FACTORY desc ";
            DataTable dt = ExcuteQuery(sql);

            if (dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    tr1.Nodes[0].Nodes.Add(dr["SUB_FACTORY"].ToString());
                }
            }
        }

        private void getNodeRecurve(TreeNode tn, DataTable dt)
        {
            foreach (DataRow dr in dt.Rows)
            {
                if (tn.Text == dr["SUB_FACTORY"].ToString())
                {
                    tn.Nodes.Add(dr["WORKSTATION"].ToString());
                }
            }

            foreach (TreeNode tnSub in tn.Nodes)
            {
                getNodeRecurve(tnSub, dt);
            }
        }

        private void addTreeViewSubNode(TreeView tr1)
        {
            string sql = @" select distinct SUB_FACTORY, WORKSTATION
                            from LamexMESManager.dbo.View_OP_Resource_Plan
                            order by SUB_FACTORY desc, WORKSTATION ";
            DataTable dt = ExcuteQuery(sql);

            if (dt.Rows.Count > 0)
            {
                foreach (TreeNode node in tr1.Nodes)
                {
                    getNodeRecurve(node, dt);
                }
            }
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            try
            {
                Color clr = imgBack.GetPixel(e.X + 10, e.Y + 10);         //获取在参考图对应坐标像素点的颜色信息
                myInfo = Lamex.Common.GetCheckRGBHelper.GetCheckRGB(clr, dtRGB);
                if (myInfo.Contains("无效区域"))
                {
                    pictureBox1.Cursor = Cursors.Arrow;
                    myText.Text = "";
                    myText.Visible = false;
                }
                else
                {
                    pictureBox1.Cursor = Cursors.Hand;          //设置手形
                    myText.Text = myInfo;
                    myText.Visible = true;
                    set_hand_shape(e.X, e.Y, myInfo);
                }
            }
            catch
            {
            }
            //if (clr.ToArgb() == Color.FromArgb(255, 0x00, 0xFF, 0x00).ToArgb())//生产_桌子  00,FF,00
            //if (clr.ToArgb() == Color.FromArgb(255, 0, 255, 0).ToArgb())//生产_桌子  00,FF,00
            //{
            //    myText.Text = "生产_桌子" + "\r\n" + "当前产量：9999pcs" + "\r\n" + "当前欠货：9999pcs";
            //    set_hand_shape(e.X, e.Y,msg);
            //}
            //else
            //{
            //    //pictureBox1.Cursor = Cursors.Arrow;
            //    //myText.Visible = false;
            //    //myText.Text = "其它区域";
            //}
        }

        private void set_hand_shape(int X,int Y,string msg)
        {
            //设置label位置
            if (X + 20 + myText.Width >= pictureBox1.Width)
                myText.Left = X - 20 - myText.Width;
            else
                myText.Left = X + 20;
            if (Y + 20 + myText.Height >= pictureBox1.Height)
                myText.Top = Y - 20 - myText.Height;
            else
                myText.Top = Y + 20;

            //ToolTip tip = new ToolTip();
            //tip.IsBalloon = true;
            //tip.SetToolTip(myText, msg);
            //tip.Show(msg, myText, 2 * 1000);        
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            Point screenPoint = Control.MousePosition;//鼠标相对于屏幕左上角的坐标
            Point formPoint = this.PointToClient(Control.MousePosition);//鼠标相对于窗体左上角的坐标
            //Point contextMenuPoint = contextMenuStrip1.PointToClient(Control.MousePosition); //鼠标相对于contextMenuStrip1左上角的坐标

            Color clr = imgBack.GetPixel(MousePosition.X, MousePosition.Y - 100);         //获取在参考图对应坐标像素点的颜色信息
            //Color clr = imgBack.GetPixel(formPoint.X, formPoint.Y - 100);         //获取在参考图对应坐标像素点的颜色信息
            myInfo = Lamex.Common.GetCheckRGBHelper.GetCheckRGB(clr, dtRGB);
            if (myInfo.Contains("生产-桌子"))
            {
                MessageBox.Show("开发中......");
                //MdiTools.OpenChildForm(this.MdiParent as IMdiForm, typeof(frmMES_Layout_Laminate), sender as ToolStripMenuItem);
            }
            else if (myInfo.Contains("生产-屏风"))
            {
                //用OpenChildForm方法RibbonForm()报错，所以改用OpenChildWebBrowse()，但仍有子窗体无法关闭Bug
                //MdiTools.OpenChildForm(this.MdiParent as IMdiForm, typeof(frmMES_Layout_Panel), sender as ToolStripMenuItem);
                MdiTools.OpenChildWebBrowse(this.MdiParent as IMdiForm, new frmMES_Layout_Panel(), sender as ToolStripMenuItem);
            }
            else if (myInfo.Contains("生产-椅子"))
            {
                //MdiTools.OpenChildForm(this.MdiParent as IMdiForm, typeof(frmMES_Layout_Seating), sender as ToolStripMenuItem);
                MdiTools.OpenChildWebBrowse(this.MdiParent as IMdiForm, new frmMES_Layout_Seating(), sender as ToolStripMenuItem);
            }
            else if (myInfo.Contains("生产-MCG"))
            {
                MessageBox.Show("开发中......");
                //MdiTools.OpenChildForm(this.MdiParent as IMdiForm, typeof(frmMES_Layout_MCG), sender as ToolStripMenuItem);
            }
            else if (myInfo.Contains("待规划"))
            {
                Loginer.CurrentUser.FunctionId = "menuSCH_ScanFollowUp";
                MdiTools.OpenChildWebBrowse(this.MdiParent as IMdiForm, new frmMES_Kanban_Scanning("T01"), sender as ToolStripMenuItem);
            }
            else
            {
                MessageBox.Show("屏幕分辨率不正确！系统支持以下分辨率：" + "\r\n"
                    + "1920x1080" + "\r\n"
                    + "1600x900" + "\r\n"
                    + "1366x768");
            }

                //if (clr.ToArgb() != Color.Black.ToArgb())
                //if (clr.ToArgb() != Color.White.ToArgb())
                //{   
                //    if (clr.ToArgb() == Color.FromArgb(255, 0x00, 0xFF, 0x00).ToArgb())//生产_桌子  00,FF,00
                //        MdiTools.OpenChildForm(this.MdiParent as IMdiForm, typeof(frmMES_Layout_Laminate), sender as ToolStripMenuItem);

                //    else 
                //        MdiTools.OpenChildForm(this.MdiParent as IMdiForm, typeof(frmMES_Layout_Panel), sender as ToolStripMenuItem);
                //}
            }

        private void cmsMES_BackgroundToggle_Click(object sender, EventArgs e)
        {
            if (Convert.ToString(pictureBox1.Tag) == "Front IMG")
            {
                pictureBox1.Image = imgBack;
                pictureBox1.Tag = "Back IMG";
            }
            else
            {
                pictureBox1.Image = imgFront;
                pictureBox1.Tag = "Front IMG";
            }
        }

        private void frmMES_Layout_Lamex_Resize(object sender, EventArgs e)
        {
            pictureBox1.Image = imgFront;       //加载图像
            //dataGridView1.Height = this.Height - 5;
            treeView1.Height = this.Height;
            button1.Location = new Point(this.Width - button1.Size.Width, button1.Location.Y);
        }

        private void treeView1_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            string strCode = "", strDesc = "";
            if (e.Node.Text.Length > 7)
            {
                strCode = e.Node.Text.Substring(3, 3);
            }
            else
            {
                return;
            }

            switch (e.Node.Text.ToString())
            {
                default:
                    MessageBox.Show("非工作站模式！");
                    break;
                case "WS_TT0_按压看板":
                case "WS_S06":
                case "WS_S08":
                case "WS_S19":
                    Loginer.CurrentUser.FunctionId = "menuSCH_ScanFollowUp";
                    //MdiTools.OpenChildWebBrowse(this.MdiParent as IMdiForm, new FrmDataCollection(strCode), sender as ToolStripMenuItem);
                    MdiTools.OpenChildWebBrowse(this.MdiParent as IMdiForm, new frmMES_Kanban_Presssing(strCode), sender as ToolStripMenuItem);
                    break;
                case "WS_TT1_直扫看板":
                case "WS_DXX_BIMA":
                case "WS_DXX_线下":
                case "WS_DXX_主线":
                case "WS_DXX_组装包装":
                case "WS_MXX_钢柜":
                case "WS_MXX_铁屏风":
                case "WS_P05":
                case "WS_P21_喷粉":
                case "WS_PXX_铝材其他":
                case "WS_PXX_屏风布板":
                case "WS_PXX_组装包装":
                case "WS_SXX_裁车开棉":
                case "WS_SXX_配件及上卡板":
                case "WS_SXX_其他CELL":
                case "WS_XXX_配件":
                    Loginer.CurrentUser.FunctionId = "menuSCH_ScanFollowUp";
                    MdiTools.OpenChildWebBrowse(this.MdiParent as IMdiForm, new frmMES_Kanban_Scanning(strCode), sender as ToolStripMenuItem);
                    break;
            }
        }

        private void tsmExpand_All_Click(object sender, EventArgs e)
        {
            treeView1.ExpandAll();

            //for (int i = treeView1.GetNodeCount(false)-1; i >-1; i--)
            //{
            //    treeView1.SelectedNode = treeView1.Nodes[i];
            //    treeView1.SelectedNode.ExpandAll();
            //}

        }

        private void tsmExpand_Assign_Click(object sender, EventArgs e)
        {
            Expand_to_Assign_Level();
        }

        private void Expand_to_Assign_Level()
        {
            treeView1.CollapseAll();
            foreach (TreeNode n in treeView1.Nodes)
            {
                if (n.Level == 0)
                {
                    treeView1.SelectedNode = n;
                    n.Expand();
                    return;
                }
            }
            //(e.Node.Level == 1)
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //用OpenChildForm方法RibbonForm()报错，所以改用OpenChildWebBrowse()，但仍有子窗体无法关闭Bug
            //MdiTools.OpenChildForm(this.MdiParent as IMdiForm, typeof(frmMES_Layout_Vector), sender as ToolStripMenuItem);
            //MdiTools.OpenChildWebBrowse(this.MdiParent as IMdiForm, new frmMES_Layout_Vector(), sender as ToolStripMenuItem);

            frmBaseChildWinForm dlg = new frmBaseChildWinForm("MES_Layout_Block_Color", 0, true, true);
            dlg.Show();
        }
    }
}